package cn.com.traffictest;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TrafficTestActivity extends Activity {
	
	private EditText mEditText;
	private Button mButton;
	private TextView mTextView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mEditText = (EditText)findViewById(R.id.server_addr);
        mButton = (Button)findViewById(R.id.start_end_button);
        mTextView = (TextView)findViewById(R.id.status);
        
        max = MAX_PACKET;
        mButton.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String order = TrafficTestActivity.this.mButton.getText().toString();
				if(order.equals("start"))
				{
					String addr = TrafficTestActivity.this.mEditText.getText().toString();
					if(addr == null)
					{
						return;
					}
					Message msg = new Message();
					msg.what = START;
					TrafficTestActivity.this.init();
					TrafficTestActivity.this.mButton.setText(R.string.stop_testing);
				}
				else if(order.equals("stop"))
				{
					max = count;
				}
			}
        	
        });
    }
    
    private Socket socket;
    public final int START = 1;
    public final int STOP = 2;
    public final int MAX_PACKET = 2000;
    
    private int count = 0;
    private int max;
    
    private OutputStream print;
    
    private void init()
    {
    	String addr = mEditText.getText().toString();
		if(addr == null)
		{
			Log.e("TrafficTestActivity.mHandler.handleMessage", "addr is null!");
		}
		else
		{
			try {
				mTextView.setText("connecting...");
				socket = new Socket(addr, 1234);
				mTextView.setText("connected!");
				print = socket.getOutputStream();
				Message message = new Message();
				message.what = START;
				mHandler.sendMessageDelayed(message, 3000);
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				Log.e("TraffciTestActivity.mHandler.handleMessage", "UnknownHost");
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
    
    private void sendAByte() throws IOException
    {
    	byte b = (byte) (count % 265);
    	print.write(b);
    	mTextView.setText("send the " + count + "th byte" + " " + b);
    	count = count + 1;
    	if(count < max)
    	{
    		Message msg = new Message();
    		msg.what = START;
    		mHandler.sendMessageDelayed(msg, 5000);
    	}
    }
    Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == TrafficTestActivity.this.START)
			{
				try {
					TrafficTestActivity.this.sendAByte();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(msg.what == TrafficTestActivity.this.STOP)
			{
				TrafficTestActivity.this.max = 0;
			}
		}
    	
    };
}